/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Rush;
import com.hboxs.asl.service.ProductService;
import com.hboxs.asl.service.RushService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Controller - 限时抢购
 */
@Controller("adminRushController")
@RequestMapping("/asl-admin/rush")
public class RushController extends BaseController {

    @Resource(name = "rushServiceImpl")
    private RushService rushService;
    @Resource(name = "productServiceImpl")
    private ProductService productService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/admin/rush/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Rush rush, String sn, RedirectAttributes redirectAttributes) {
        if (!isValid(rush)) {
            return ERROR_VIEW;
        }
        if (rush.getBeginDate()==null){
            rush.setBeginDate(new Date());
        }
        rush.setProduct(productService.findBySn(sn));
        rushService.save(rush);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("rush", rushService.find(id));
        return "/admin/rush/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Rush rush, RedirectAttributes redirectAttributes) {
        if (!isValid(rush)) {
            return ERROR_VIEW;
        }
        rushService.update(rush, "");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", rushService.findPage(pageable));
        return "/admin/rush/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        rushService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
